<?php
	defined('_JEXEC') or die('Restricted access');
	require_once(CUTEENGINE_BASE_FIELD_PATH."base_field_class.php");
    
	//
    // t_field_do_it
    //
	class t_field_do_it	extends t_base_field
	{
        // property declaration
		// public
		const language_position_label = 1;
		const language_position_button = 2;
		const language_position_label_per_initialization_correct_result = 3;
		const language_position_button_per_initialization_correct_result = 4;
		const language_position_label_per_initialization_error_result = 5;
		const language_position_button_per_initialization_error_result = 6;
		const language_position_label_per_do_it_correct_result = 7;
		const language_position_button_per_do_it_correct_result = 8;
		const language_position_label_per_do_it_error_result = 9;
		const language_position_button_per_do_it_error_result = 10;	
		const language_position_do_it_start_meggase = 11;			
		const language_position_last = 12;
		
		// P
        const proper_properties_position_per_initialization_query = 1;
		const proper_properties_position_per_initialization_query_parameters_names = 2; 
        const proper_properties_position_per_initialization_query_result_file_name = 3; // does not include extension
        const proper_properties_position_per_do_it_query = 4;
		const proper_properties_position_per_do_it_query_parameters_names = 5; 
        const proper_properties_position_per_do_it_query_result_file_name = 6; // does not include extension
        const proper_properties_position_per_use_custom_language = 7;
        const proper_properties_position_last = 8;        
        // property declaration
		// protected	
		// B
		protected $button_tag = null;
		// C 
		protected $container_tag = null;
		// I
		protected $ids_when_used_as_included = Array();
		// L
		protected $label_tag = null;
		
        // method declaration
    	// protected
		// C
		protected function complete_field_tags($a_button_text,$a_label_text)
        {			
			//echo  "a_label_text: $a_label_text - a_button_text: $a_button_text<br>";
			// label tag
			
			$this->label_tag->set_text_to_include(
                $a_label_text);
			if ($this->sql_do_it_exist())
			{
				// button tag
				//print_r($this->button_tag);
				$this->button_tag->set_text_to_include(
					$a_button_text);
			}		

		}	
		protected function complete_field_tags_by_javascript($a_action,$a_button_text,$a_label_text,$a_lbl_class_name = '',$a_btn_class_name = '')
        {			
			global $g_javascript_namespace;

			
			$local_button_string = "";
			$local_button_style_string = "";
			$local_label_style_string = "";
			$local_label_string = "";
			$local_declaretion .= "";
			
			foreach ($this->ids_when_used_as_included As $local_index => $local_id)
			{
				//echo "$local_id - ".$this->button_tag->name."<br>";
					
				
				
				$local_declaretion .= "var local_row_$local_index = window.cuteengine.html_tag('".$local_id."');";
				if ($this->sql_do_it_exist())
				{
					$local_button_string .= "local_row_$local_index.childNodes[0].innerHTML = '$a_button_text';";
					if ($a_btn_class_name != '')
						$local_button_style_string .= "local_row_$local_index.childNodes[0].className = '$a_btn_class_name';";
				}
				if ($a_lbl_class_name != '')
					$local_label_style_string .= "local_row_$local_index.childNodes[1].className = '$a_lbl_class_name';";
				$local_label_string .= "local_row_$local_index.childNodes[1].innerHTML = '$a_label_text';";
				//
				
			}
			return "$local_declaretion
			$local_button_string
			$local_button_style_string
			$local_label_string
			$local_label_style_string";
			
		}		
		
        // G
		protected function get_action_name($a_prefix)
        {
            return $a_prefix . "_" . $this->name;
        } 
		protected function get_field_event_script($a_event_type,$a_argouments)
        {			
			//echo "a_event_type:$a_event_type<BR>";
			$local_return = '';
			
			switch ($a_event_type) 
            {
				
				case t_base_field::field_event_type_on_selected:
					if ($this->sql_do_it_exist())
					{	
						$this->default_start_tag->add_property("onclick",t_cuteengine::javascript_instance_name.".".$this->name.".".$this->get_event_name($a_event_type)."($a_argouments)");
					}
				break;       
				
			}
			return $local_return;
		}
                
        protected function get_language_js_array()
        {
            return '["'.implode('","',$this->lang).'"]';
        }    
				
		public function get_included_in_html_structure_no_edit_mode($a_id,$a_value,$a_label_value)
		{
			global $g_url_parameters_namespace;
			global $g_cuteengine;
			 
			//$this->uses_as_included_in = true;
			$this->ids_when_used_as_included[] = $a_id;
			
			$local_param = $g_url_parameters_namespace->get_parameter_by_component_id($this->parent_component->id);
			$local_param->argument = $a_value;
			
			//echo $this->container_tag->generate_html_code_with_new_name($a_name);
			//echo  "proper_properties_position_per_do_it_query_parameters_names: ". $this->proper_properties_array[self::proper_properties_position_per_do_it_query_parameters_names];
			$this->get_field_event_script(t_base_field::field_event_type_on_selected,"event,$a_value");
			
						
			return $this->container_tag->generate_html_code_with_new_name($a_id);
		}		
static function get_help_proper_property($a_index)
		{
			$local_return = 'NO HELP AVAILABLE';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = '';
				break;                
			}*/
			return $local_return;
		}
        static function get_default_proper_property($a_index)
		{
			$local_return = '';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = 'TRUE';
				break;                
			}*/
			return $local_return;
		}
		protected function get_proper_properties_position_last()      
		{
            return self::proper_properties_position_last;
        }
		// I
		protected function is_there_initialization_query()
        {
            return (trim($this->proper_properties_array[self::proper_properties_position_per_initialization_query]) != '');
        }		
        // method declaration
    	// public
        public function __construct($a_parent_part,$a_common_properties_str, $a_proper_properties_str) 
		{
            global $g_ambient_namespace;
            parent::__construct($a_parent_part,$a_common_properties_str, $a_proper_properties_str);
            // LANGUAGES
            if (strtoupper($this->proper_properties_array[self::proper_properties_position_per_use_custom_language]) == 'TRUE')
                $g_ambient_namespace->load_custom_language($this,$this->name);
            // initialization
			if (substr($this->proper_properties_array[self::proper_properties_position_per_initialization_query_parameters_names],0,1) == '[')
				$local_params = $this->proper_properties_array[self::proper_properties_position_per_initialization_query_parameters_names];
			else
				$local_params = '["'.str_replace(t_strings_utility_namespace::separator_level_2, '","', substr($this->proper_properties_array[self::proper_properties_position_per_initialization_query_parameters_names],1,-1)).'"]';
            $this->parent_part->add_action_for_field_uses_as_included('["none","t_action_execute_query","'.$this->get_action_name('initialize').'",["'.$this->name.'"],"'.
				$this->proper_properties_array[self::proper_properties_position_per_initialization_query].'",'.$local_params.',"FALSE","FALSE"]');
            // do_it    
			if ($this->sql_do_it_exist())
            {	
				//echo $this->get_action_name('do_it');
				if (substr($this->proper_properties_array[self::proper_properties_position_per_do_it_query_parameters_names],0,1) == '[')
					$local_params = $this->proper_properties_array[self::proper_properties_position_per_do_it_query_parameters_names];
				else
					$local_params = '["'.str_replace(t_strings_utility_namespace::separator_level_2, '","', substr($this->proper_properties_array[self::proper_properties_position_per_do_it_query_parameters_names],1,-1)).'"]';
				//echo "p: ".$this->proper_properties_array[self::proper_properties_position_per_do_it_query_parameters_names]." - local_params:$local_params<br>"; 
				//echo "this->name: $this->name";
				$this->parent_part->add_action_for_field_uses_as_included('["none","t_action_execute_query","'.$this->get_action_name('do_it').'",["'.$this->name.'"],"'.
					$this->proper_properties_array[self::proper_properties_position_per_do_it_query].'",'.$local_params.',"TRUE","TRUE"]');
			}
			
		}
		// A
		public function apply_result_script($a_query_parameter,$a_current_row_no,$a_rows_count,$a_arguments)
        {
			if (isset($a_arguments))
			{
				global $g_javascript_namespace;
				$local_action = $a_arguments[0];
				$local_action_name = $local_action->get_action_name();
				//echo "local_action_name: $local_action_name<br>";
				$local_result = $a_arguments[1];
			   // print_r($local_result);
			   //echo "$local_action_name == ".$this->get_action_name('initialize')."<br>";
				if (($local_action_name == $this->get_action_name('initialize')) ||
					($local_action_name == $this->get_action_name('initialize')."_reload"))
				{
					$local_file_name = trim(str_replace('.php','',$this->proper_properties_array[self::proper_properties_position_per_do_it_query_result_file_name]));
					//echo "local_file_name: $local_file_name<br>";
					if ($local_file_name != '')
					{
						$local_file_name = CUTEENGINE_PATH_CUSTOM_FILES.DIRECTORY_SEPARATOR.$local_file_name.".php";
						require($local_file_name);
					}
					else
					{
						//echo "a_query_parameter->sql_values[0]:". $a_query_parameter->sql_values[0]."<br>";
						if ((($a_query_parameter->is_query_without_result()) && ($a_query_parameter->sql_result_query)) || ($a_query_parameter->sql_values[0] == 1))
							if ($this->uses_as_included_in)
							{
								
								$local_script = $this->complete_field_tags_by_javascript($local_action,$this->lang[self::language_position_button_per_initialization_correct_result],
									$this->lang[self::language_position_label_per_initialization_correct_result]);
								if ($local_action_name == $this->get_action_name('initialize'))
								{
									$g_javascript_namespace->header_script_generator->add_content_to_a_method(
										get_class($this),t_header_script_generator::create_label,"
		if (this.name == '".$this->name."')
		{
			$local_script
		}");
								}	
								else
								{
									$local_result->add_function_result(
										"","","",$local_script);    								
								}
							}	
							else
								$this->complete_field_tags($this->lang[self::language_position_button_per_initialization_correct_result],
								$this->lang[self::language_position_label_per_initialization_correct_result]);
						else
							if ($this->uses_as_included_in)
							{
								$local_script = $this->complete_field_tags_by_javascript($local_action,$this->lang[self::language_position_button_per_initialization_error_result],
									$this->lang[self::language_position_label_per_initialization_error_result]);
							
								if ($local_action_name == $this->get_action_name('initialize'))
								{
									$g_javascript_namespace->header_script_generator->add_content_to_a_method(
										get_class($this),t_header_script_generator::create_label,"
		if (this.name == '".$this->name."')
		{
			$local_script
		}");
								}	
								else
								{
									$local_result->add_function_result(
										"","","",$local_script);    								
								}	
							}
							else
								$this->complete_field_tags($this->lang[self::language_position_button_per_initialization_error_result],
								$this->lang[self::language_position_label_per_initialization_error_result]);
						
					}
				}
				else if (($this->sql_do_it_exist()) && ($local_action_name == $this->get_action_name('do_it')))
				{
					$local_file_name = trim(str_replace('.php','',$this->proper_properties_array[self::proper_properties_position_per_do_it_query_result_file_name]));
					if ($local_file_name != '')
					{
						echo "local_file_nam: $local_file_name<br>";
						$local_file_name = CUTEENGINE_PATH_CUSTOM_FILES.DIRECTORY_SEPARATOR.$local_file_name.".php";
						require($local_file_name);
					}
					else
					{
						if ($a_query_parameter->sql_values[0] > 1) 
						{
							$local_button_text = $this->lang[self::language_position_button_per_do_it_correct_result];
							$local_label_text = $this->lang[self::language_position_label_per_do_it_correct_result];
						}        
						else
						{
							$local_button_text = $this->lang[self::language_position_button_per_do_it_error_result];
							$local_label_text = $this->lang[self::language_position_label_per_do_it_error_result];   
						}        
						$local_result->add_function_result(
							"","","","window.cuteengine.html_tag(window.cuteengine.".$this->name.".button_tag).innerHTML = '$local_button_text';".
			"window.cuteengine.html_tag(window.cuteengine.".$this->name.".label_tag).innerHTML = '$local_label_text';");        
					}
				
				}
			}
			else
			{
				
       
				$local_action = $a_query_parameter;
				$local_action_name = $local_action->get_action_name();
				//echo "local_action_name: $local_action_name<br>";
				$local_result = $a_current_row_no;
				$local_file_name = trim(str_replace('.php','',$this->proper_properties_array[self::proper_properties_position_per_do_it_query_result_file_name]));
				if ($local_file_name != '')
				{
					echo "local_file_nam: $local_file_name<br>";
					$local_file_name = CUTEENGINE_PATH_CUSTOM_FILES.DIRECTORY_SEPARATOR.$local_file_name.".php";
					require($local_file_name);
				}
			}
		}
		public function apply_error_result_script($a_query_parameter,$a_current_row_no,$a_rows_count,$a_arguments)
        {
			if (isset($a_arguments))
			{
				$local_action = $a_arguments[0];
				$local_action_name = $local_action->get_action_name();
				//echo "local_action_name: $local_action_name<br>";
				$local_result = $a_arguments[1];
			   // print_r($local_result);
				if (($local_action_name == $this->get_action_name('initialize')) ||
					($local_action_name == $this->get_action_name('initialize')."_reload"))
				{
					$local_file_name = trim(str_replace('.php','',$this->proper_properties_array[self::proper_properties_position_per_do_it_query_result_file_name]));
					//echo "local_file_name: $local_file_name<br>";
					if ($local_file_name != '')
					{
						$local_file_name = CUTEENGINE_PATH_CUSTOM_FILES.DIRECTORY_SEPARATOR.$local_file_name.".php";
						require($local_file_name);
					}
					else
					{
						//echo "a_query_parameter->sql_values[0]:". $a_query_parameter->sql_values[0]."<br>";
						if ((($a_query_parameter->is_query_without_result()) && ($a_query_parameter->sql_result_query)) || ($a_query_parameter->sql_values[0] == 1))
							$this->complete_field_tags($this->lang[self::language_position_button_per_initialization_correct_result],
								$this->lang[self::language_position_label_per_initialization_correct_result]);
						else
							$this->complete_field_tags($this->lang[self::language_position_button_per_initialization_error_result],
								$this->lang[self::language_position_label_per_initialization_error_result]);
						
					}
				}
				else if (($this->sql_do_it_exist()) && ($local_action_name == $this->get_action_name('do_it')))
				{
					$local_file_name = trim(str_replace('.php','',$this->proper_properties_array[self::proper_properties_position_per_do_it_query_result_file_name]));
					if ($local_file_name != '')
					{
						//echo "local_file_nam: $local_file_name<br>";
						$local_file_name = CUTEENGINE_PATH_CUSTOM_FILES.DIRECTORY_SEPARATOR.$local_file_name.".php";
						require($local_file_name);
					}
					else
					{
						if ($a_query_parameter->sql_values[0] > 1) 
						{
							$local_button_text = $this->lang[self::language_position_button_per_do_it_correct_result];
							$local_label_text = $this->lang[self::language_position_label_per_do_it_correct_result];
						}        
						else
						{
							$local_button_text = $this->lang[self::language_position_button_per_do_it_error_result];
							$local_label_text = $this->lang[self::language_position_label_per_do_it_error_result];   
						}        
						$local_result->add_function_result(
							"","","","window.cuteengine.html_tag(window.cuteengine.".$this->name.".button_tag).innerHTML = '$local_button_text';".
			"window.cuteengine.html_tag(window.cuteengine.".$this->name.".label_tag).innerHTML = '$local_label_text';");        
					}
				
				}
			}
			else
			{
				
       
				$local_action = $a_query_parameter;
				$local_action_name = $local_action->get_action_name();
				//echo "local_action_name: $local_action_name<br>";
				$local_result = $a_current_row_no;
				$local_file_name = trim(str_replace('.php','',$this->proper_properties_array[self::proper_properties_position_per_do_it_query_result_file_name]));
				if ($local_file_name != '')
				{
					echo "local_file_nam: $local_file_name<br>";
					$local_file_name = CUTEENGINE_PATH_CUSTOM_FILES.DIRECTORY_SEPARATOR.$local_file_name.".php";
					require($local_file_name);
				}
			}
		}
		
		// C		
		public function create_tags_structure()
        {
            global $g_javascript_namespace;
           // echo __METHOD__."<BR>";
            parent::create_tags_structure();
			
			//echo "this->uses_as_included_in: $this->uses_as_included_in<br>";
			if ($this->uses_as_included_in)
				{
					/* create an action bound to reload action of grid */
					$local_reload_action_name = $this->parent_part->get_action_name_of_part_event(t_base_field::field_event_type_on_reload,"t_action_set_values_from_db");
					//$local_action = $g_cuteengine->actions_list[md5($local_reload_action_name)];
					//echo "local_reload_action_name: $local_reload_action_name<br>";
					//if (isset($local_action))
					{
						
						 // initialization
						if (substr($this->proper_properties_array[self::proper_properties_position_per_initialization_query_parameters_names],0,1) == '[')
							$local_params = $this->proper_properties_array[self::proper_properties_position_per_initialization_query_parameters_names];
						else
							$local_params = '["'.str_replace(t_strings_utility_namespace::separator_level_2, '","', substr($this->proper_properties_array[self::proper_properties_position_per_initialization_query_parameters_names],1,-1)).'"]';
						
						$this->parent_part->add_action_for_field_uses_as_included('["'.$local_reload_action_name.'","t_action_execute_query","'.$this->get_action_name('initialize').'_reload",["'.$this->name.'"],"'.
							$this->proper_properties_array[self::proper_properties_position_per_initialization_query].'",'.$local_params.',"FALSE","FALSE"]');
						
					}
				}	
			// proprierties
			// C
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'current_tag',"null",false);
			// B
			if ($this->sql_do_it_exist())
			{
				$g_javascript_namespace->header_script_generator->add_property_to_a_class(
					get_class($this),'button_tag',"this.name + '_button'",false);
			}		
			// L
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'label_tag',"this.name + '_label'",false);
			
			// tags
			$this->container_tag = parent::add_tag(t_base_tag_type::btt_div,
				$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_container");

            if ($this->sql_do_it_exist())
			{
				// button tag
				$this->button_tag =  $this->container_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_button,
					$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_button");
				
				$this->default_start_tag = $this->button_tag;
			}	
			// label tag
            $this->label_tag =  $this->container_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_label,
                $this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_label");
            //$this->default_start_tag = $this->label_tag;	

			// event
			
			$this->get_field_event_script(t_base_field::field_event_type_on_selected,"event");
            
           	// event
			//$this->get_field_event_script(t_base_field::field_event_type_on_selected,"event");
           
           
			
			if (! $this->is_there_initialization_query())
			{
				
				$this->complete_field_tags($this->lang[self::language_position_button],
					$this->lang[self::language_position_label]);
			}
			else
			{
				
			}
			
        }
		// C
		public function create_javascript_initialization_component()
		{
		}
        public function finalize_creation_field()
        {
			
            global $g_cuteengine;
			parent::finalize_creation_field();
			if ( ($this->is_there_initialization_query()))
			{
				
				
				//echo "finalize_creation_field<br>";
                $local_result = new t_content_loader_result;
				$local_action = $g_cuteengine->actions_list[md5($this->get_action_name('initialize'))];
				$local_action->do_action_from_ajax($local_result);
				if (! $local_result->succeeded)
				{
					throw new Exception("ERROR, azione initialization eseguita con errore!");
				}
				
			}
		}	
        // G
        public function get_event_name()
        {

            return /*get_class($this)*/$this->name . "_event_".$this->get_event_type('');
        }
        public function get_event_type($a_action_name)
        {	
			return t_base_field::field_event_type_on_selected;
			
		}
		public function get_language_position_last()
        {
            return self::language_position_last;
        }
        public function get_result_action_script($a_event_type,$parent_event_type,
            $a_parameter,$a_content_loader_result)
		{
            return '';
        }
        // I
		public function initialize_field_event($a_event_type)
        {			
			global $g_javascript_namespace;
            global $g_strings_utility_namespace;
            global $g_url_parameters_namespace;
            $local_parameter = $g_url_parameters_namespace->get_parameter_by_component_id($this->parent_component->id);
			$local_event = $this->events[$a_event_type];
			//echo "local_event->event_name: ".$local_event->event_name." - local_event->event_type: $local_event->event_type<br>";
			switch ($local_event->event_type) 
            {
                /***********  t_base_field::field_event_type_on_selected  ***********/                
				case t_base_field::field_event_type_on_selected:
					$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
						get_class($this),$local_event->event_name,'event, a_argument_parent_field',"
	    var local_target = event.target ? event.target : event.srcElement;
	    this.current_tag = local_target.parentNode;
	    if ((a_argument_parent_field != undefined) && (a_argument_parent_field != null))
			var local_url_arg = a_argument_parent_field;
	   else
			var local_url_arg = '".str_replace(t_url_parameter::separator_level_0, t_url_parameter::separator_level_1,$local_parameter->argument)."';
	   set_tag_property(['innerHTML'],['".$this->lang[self::language_position_do_it_start_meggase]."'],this.current_tag.childNodes[1],false);
	   var local_argoument = '';
	   if (local_url_arg != '')
			var local_argoument = '".t_url_parameter::separator_level_0 . "' + local_url_arg;
       ".$local_event->body_code."
       ");
				break;

			}	
            
		}

		public function initialize_main_tag()
        {	
            $this->main_tag = parent::get_parent_tag(); 
			$this->default_start_tag = $this->main_tag;
		}
					
		// S
		public function sql_do_it_exist()
		{
            $local_sql_do_it = trim(substr($this->proper_properties_array[self::proper_properties_position_per_do_it_query],1,-1));
			return ($local_sql_do_it != '');
		}	

        
    }
?>    